

from fastapi import FastAPI
# 实现文件上传
from fastapi import File, UploadFile

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": f"接口id：{item_id}","q_context":f"q= {q}"}


@app.post("/upload-file/")
async def upload_file(file: UploadFile):
    file_content = await file.read()
    with open("new_filename.txt", "wb") as new_file:
        new_file.write(file_content)

    await file.close()


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)


    